local sk__dawu = fk.CreateSkill {

  name = "sk__dawu",

  tags = {},

}



sk__dawu:addEffect(fk.EventPhaseStart, {
  name = "sk__dawu",
  anim_type = "defensive",
  expand_pile = "sk__godzhugeliang_star",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__dawu.name) then
      return target == player and player.phase == Player.Finish and #player:getPile("sk__godzhugeliang_star") > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local success, dat = player.room:askForUseActiveSkill(player, "sk__dawu_active", "#sk__dawu-choose", true, nil, false)
    if success then
      event:setCostData(self, dat)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, sk__dawu.name, event:getCostData(self).targets)
    room:moveCardTo(event:getCostData(self).cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, sk__dawu.name, nil,
      true, player)
    for _, id in ipairs(event:getCostData(self).targets) do
      local p = room:getPlayerById(id)
      if not p.dead then
        room:addPlayerMark(p, "@@sk__dawu", 1)
      end
    end
  end,
})
sk__dawu:addEffect(fk.DamageInflicted, {
  name = "sk__dawu",
  anim_type = "defensive",
  expand_pile = "sk__godzhugeliang_star",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__dawu.name) then
      return target:getMark("@@sk__dawu") > 0 and data.damageType ~= fk.ThunderDamage and
          table.contains(player:getTableMark(sk__dawu.name), target.id)
    end
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    target:drawCards(1, sk__dawu.name)
    data:preventDamage()
  end,
})
sk__dawu:addEffect(fk.TurnStart, {
  can_refresh = function(self, event, target, player, data)
    if target == player and player:getMark(sk__dawu.name) ~= 0 then
      if event == fk.EventLoseSkill then
        return data == self
      else
        return true
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, id in ipairs(player:getMark(sk__dawu.name)) do
      room:setPlayerMark(room:getPlayerById(id), "@@sk__dawu", 0)
    end
    room:setPlayerMark(player, sk__dawu.name, 0)
  end,
})
sk__dawu:addEffect(fk.EventLoseSkill, {
  can_refresh = function(self, event, target, player, data)
    if target == player and player:getMark(sk__dawu.name) ~= 0 then
      if event == fk.EventLoseSkill then
        return data == self
      else
        return true
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, id in ipairs(player:getMark(sk__dawu.name)) do
      room:setPlayerMark(room:getPlayerById(id), "@@sk__dawu", 0)
    end
    room:setPlayerMark(player, sk__dawu.name, 0)
  end,
})
sk__dawu:addEffect(fk.Death, {
  can_refresh = function(self, event, target, player, data)
    if target == player and player:getMark(sk__dawu.name) ~= 0 then
      if event == fk.EventLoseSkill then
        return data == self
      else
        return true
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, id in ipairs(player:getMark(sk__dawu.name)) do
      room:setPlayerMark(room:getPlayerById(id), "@@sk__dawu", 0)
    end
    room:setPlayerMark(player, sk__dawu.name, 0)
  end,
})

return sk__dawu
